package cn.krund.lyc.hotlerobot.activity.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.krund.lyc.hotlerobot.R;
import cn.krund.lyc.hotlerobot.activity.base.BaseActivity;
import cn.krund.lyc.hotlerobot.activity.view.MyGiftView;
import cn.krund.lyc.hotlerobot.utils.Constant;
import cn.krund.lyc.hotlerobot.xunfei.XunfeiAlternate;

/**
 * @包名: cn.krund.lyc.hotlerobot.activity.ui
 * @作者: 刘育成
 * @时间: 2018/4/3  10:07
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: StandbyModeActivity
 */

public class StandbyModeActivity extends BaseActivity {
    @BindView(R.id.gifView)
    MyGiftView mGifView;
    private XunfeiAlternate mXunfeiAlternate;


    private static int[] bgs = {R.raw.sleepimg, R.raw.happyimg, R.raw.angerimg, R.raw.blinkimg, R.raw.cryimg};

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mGifView.setMovieResource(bgs[getRand()]);
                    mHandler.sendEmptyMessageDelayed(1, 5000);
                    mXunfeiAlternate.speak(getAwakeAnswer(Constant.depart_delivery));
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_standby_mode;
    }

    @Override
    protected void initView() {
        mGifView.setMovieResource(R.raw.happyimg);
        mHandler.sendEmptyMessageDelayed(1, 2000);

    }

    @Override
    protected void initNev() {
        mGifView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ClaimGoodsActivity.class);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mXunfeiAlternate = XunfeiAlternate.getInstance();
    }

    /**
     * 随机5张图片
     */
    int result = 0;

    private int getRand() {
        if (result == (bgs.length - 1)) {
            result = 0;
        } else {
            result++;
        }
        return result;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(1);
        mGifView.clearAnimation();
        mGifView = null;
    }

    /**
     * 获取随机回答
     *
     * @return
     */
    public String getAwakeAnswer(String[] txt) {
        int num = (int) (Math.random() * txt.length);
        return txt[num];
    }
}
